﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;


namespace ZFCNewWeb
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ZfcService
    {
        [OperationContract]
        public List<ImageInfo> GetImages()
        {
            using (BaseDBEntities context = new BaseDBEntities())
            {
                return context.ImageInfoes.Where(i => i.IsActive).Take(25).ToList();
            }

        }

        [OperationContract]
        public void SaveUserInfo(UserInfo user)
        {
            using (BaseDBEntities context = new BaseDBEntities())
            {
                var dbUser = context.UserInfoes.FirstOrDefault(u => u.Id == user.Id);
                if (dbUser == null)
                {
                    user.LastVisited = DateTime.UtcNow;
                    context.AddToUserInfoes(user);
                }
                else
                {
                    dbUser.LastVisited = DateTime.UtcNow;
                }
                context.SaveChanges();
            }
        }

        [OperationContract]
        public void AddFeedback(Feedback feedback)
        {


            using (BaseDBEntities context = new BaseDBEntities())
            {
                if (feedback.UserInfo != null)
                {
                    feedback.UserInfo = context.UserInfoes.First(u => u.Id == feedback.UserInfo.Id);
                    feedback.UserInfo.LastVisited = DateTime.UtcNow;


                    //feedback.UserInfo = null;
                }
                context.AddToFeedbacks(feedback);
                context.SaveChanges();
            }
        }

        [OperationContract]
        public List<Team> GetTeams()
        {
            using (BaseDBEntities context = new BaseDBEntities())
            {
                return context.Teams.ToList();
            }
        }

        [OperationContract]
        public List<Schedule> GetAllMatches()
        {
            using (BaseDBEntities context = new BaseDBEntities())
            {
                return context.Schedules.ToList();
            }
        }

        [OperationContract]
        public Schedule GetNextMatch()
        {
            using (BaseDBEntities context = new BaseDBEntities())
            {
                return context.Schedules.Where(s => s.Time > DateTime.UtcNow).OrderBy(s=>s.Time).FirstOrDefault();
               
            }
        }
    }
}
